package com.zyn.es.admin;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.indices.IndexAlreadyExistsException;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.zyn.es.basic.EsClient;

/**
 * 索引操作
 * 
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-admin-indices.html
 * @author tongyufu
 *
 */
public class Index extends EsClient {

    public static void main(String[] args) throws Exception {
        Index index = new Index();
        //        index.deleteIndex();
        //        index.createIndex();
        index.getIndexSettings();
    }

    /**
     * 创建索引
     */
    public void createIndex() throws Exception {
        super.initClient();
        log.info("================================");
        try {
            Builder settings = Settings.builder();
            settings.put("index.number_of_shards", "3");
            settings.put("index.number_of_replicas", 2);

            CreateIndexRequestBuilder build = client.admin().indices().prepareCreate("twitter");
            String source = FileUtils
                .readFileToString(new File(Index.class.getResource("/mapping/tweet.json").toURI()));
            build.addMapping(TYPE, source);
            log.info("===" + source);
            CreateIndexResponse response = build.get();
            log.info("reponse：" + response.isAcknowledged());
            log.info("================================");
        } catch (IndexAlreadyExistsException e) {
            log.error("索引已经存在:" + e.getMessage());
        } finally {
            client.close();
        }
    }

    /**
     * 删除索引
     */
    public void deleteIndex() {
        super.initClient();
        log.info("================================");
        try {
            DeleteIndexResponse response = client.admin().indices().prepareDelete("twitter").get();
            log.info("reponse：" + response.isAcknowledged());
            log.info("================================");
        } catch (IndexNotFoundException e) {
            log.error("索引不存在:" + e.getMessage());
        } finally {
            client.close();
        }
    }

    /**
     * 刷新索引
     */
    public void refreshIndex() {
        super.initClient();
        log.info("================================");
        //刷新所有索引
        client.admin().indices().prepareRefresh().get();
        //刷新单个索引
        client.admin().indices().prepareRefresh("twitter").get();
        //刷新多个索引
        client.admin().indices().prepareRefresh("twitter", "company").get();
        log.info("================================");
        client.close();
    }

    /**
     * 获取索引设置
     */
    public void getIndexSettings() {
        super.initClient();
        log.info("================================");
        try {
            GetSettingsResponse response = client.admin().indices()
                .prepareGetSettings(INDEX, "twitter").get();
            for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) {
                String index = cursor.key;
                Settings settings = cursor.value;
                Integer shards = settings.getAsInt("index.number_of_shards", null);
                Integer replicas = settings.getAsInt("index.number_of_replicas", null);
            }
        } catch (IndexNotFoundException e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        log.info("================================");
    }

    /**
     * 更新索引设置
     */
    public void updateIndexSettings() {
        super.initClient();
        log.info("================================");
        client.admin().indices().prepareUpdateSettings("twitter")
            .setSettings(Settings.builder().put("index.number_of_replicas", 0)).get();
        client.close();
        log.info("================================");
    }
}
